package com.pz.cloud.framework.fundApply.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.business.fundApply.entity.FundApply;
import com.pz.cloud.framework.business.fundApply.service.IFundApplyService;
import com.pz.cloud.framework.business.workflow.annotation.Workflow;
import com.pz.cloud.framework.fundApply.callback.FundApplyCallback;
import com.pz.cloud.framework.fundApply.mapper.FundApplyMapper;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FundApplyServiceImpl extends ServiceImpl<FundApplyMapper, FundApply> implements IFundApplyService {

    @Autowired
    private CommonDao commonDao;

    @Override
    public IPage<FundApply> findPage(FundApply entity, Map<String, String[]> parameterMap) {
        return commonDao.findPage(sqlBuilder(entity, parameterMap));
    }

    @Override
    public List<FundApply> list(FundApply entity, Map<String, String[]> parameterMap) {
        return commonDao.findList(sqlBuilder(entity, parameterMap));
    }

    private AbstractSqlBuilder sqlBuilder(FundApply entity, Map<String, String[]> parameterMap) {
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select fa.*,u.realname from ").appendTable(FundApply.class).append(" fa ")
                .append(" left join ").appendTable(SysUser.class).append(" u on u.id = fa.applier_id and u.del_flag = 0 ")
                .orderBy("fa.id desc")
                .returnType(FundApply.class)
                .page(entity)
                .joinCondition(entity,"fa", parameterMap)
                .filterDataScope("fa");
        return sqlBuilder;
    }

    @Override
    @Workflow(callback = FundApplyCallback.class)
    public boolean save(FundApply entity) {
        boolean flag;
        if (StringUtils.isEmpty(entity.getId())) {
            flag = super.save(entity);
        }
        else {
            flag = super.updateById(entity);
        }
        entity.getWorkflowModel().setHandlerId(entity.getInstructorId());

        return flag;
    }
}
